package m.jclecai.com.view.common;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by andy on 2017/7/20.
 * 这是一个圆角的TabLayout；
 */

public class RectangleTabLayout extends TabLayout {

    int selectedColor;
    int normalColor;
    int radius;
    int strokeWidth;

    public RectangleTabLayout(Context context) {
        this(context,null);
    }

    public RectangleTabLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RectangleTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        selectedColor=Color.RED;
        normalColor=Color.WHITE;
        radius=dip2px(5);
        strokeWidth=dip2px(1);
    }

    /**
     * 设置选中的颜色
     * @param selectedColor
     */
    public void setSelectedColor(int selectedColor) {
        this.selectedColor = selectedColor;
    }

    /**
     * 设置普通的颜色
     * @param normalColor
     */
    public void setNormalColor(int normalColor) {
        this.normalColor = normalColor;
    }

    /**
     * 设置圆角
     * @param radius
     */
    public void setRadius(int radius) {
        this.radius = radius;
    }

    /**
     * 设置边框的宽度；
     * @param strokeWidth
     */
    public void setStrokeWidth(int strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    @Override
    public void setupWithViewPager(@Nullable ViewPager viewPager) {
        super.setupWithViewPager(viewPager);
        //这里我们要进行的一个操作就是；

        LinearLayout tabLayoutChild = (LinearLayout) getChildAt(0);

        for (int i = 0; i < tabLayoutChild.getChildCount(); i++) {
            View view = tabLayoutChild.getChildAt(i);
            if(0==i){
                //最开始；
                view.setBackground(createLeftDrawable(selectedColor,normalColor));
            }else if(i==tabLayoutChild.getChildCount()-1){
                //最后
                view.setBackground(createRightDrawable(selectedColor,normalColor));
            }else{
                //中间
                if(i<tabLayoutChild.getChildCount()-2){
                    //中间非最后一个
                    view.setBackground(createNoLeftStoke(selectedColor,normalColor));
                }else{
                    //这里表示中间的最后一个背景
                    view.setBackground(createNoVeticalStoke(selectedColor,normalColor));
                }

            }
        }
    }

    public Drawable createRightDrawable(int selectColor,int normalColor){

        //右边的按钮的normal的状态；
        GradientDrawable buttonRightNormal = new GradientDrawable();
        float[] radisRight={0,0,radius,radius,radius,radius,0,0};
        buttonRightNormal.setCornerRadii(radisRight);
        buttonRightNormal.setStroke(strokeWidth,selectColor);
        buttonRightNormal.setColor(normalColor);

        //右边的按钮的selected的状态；
        GradientDrawable buttonRightSelected= new GradientDrawable();
        buttonRightSelected.setCornerRadii(radisRight);
        buttonRightSelected.setStroke(strokeWidth,selectColor);
        buttonRightSelected.setColor(selectColor);

        return createStateListDrawable(buttonRightNormal,buttonRightSelected);
    }

    public Drawable createLeftDrawable(int selectColor,int normalColor){

        //这样表示第一个按钮的边框是一个normal的状态；
        GradientDrawable buttonLeftNormal = new GradientDrawable();
        float[] radisLeft={radius,radius,0,0,0,0,radius,radius};
        buttonLeftNormal.setCornerRadii(radisLeft);
        buttonLeftNormal.setStroke(strokeWidth,selectColor);
        buttonLeftNormal.setColor(normalColor);

        //这样表示第一个按钮的边框是一个selected的状态；
        GradientDrawable buttonLeftSelected = new GradientDrawable();
        buttonLeftSelected.setCornerRadii(radisLeft);
        buttonLeftSelected.setStroke(strokeWidth,selectColor);
        buttonLeftSelected.setColor(selectColor);

        return createStateListDrawable(buttonLeftNormal,buttonLeftSelected);
    }


    /**
     * 创建一个没有左右边框的背景选择器；
     * @param selectColor
     * @param normalColor
     * @return
     */
    public Drawable createNoVeticalStoke(int selectColor, int normalColor){


        //======创建中间按钮的背景；========
        //中间按钮的normal状态；
        GradientDrawable centerButttonNormal1= new GradientDrawable();
        centerButttonNormal1.setStroke(strokeWidth,selectColor);
        //centerButttonNormal.setStroke();
        centerButttonNormal1.setColor(normalColor);

        //中间按钮的normal状态；
        GradientDrawable centerButttonNormal2= new GradientDrawable();
        //centerButttonNormal.setStroke();
        centerButttonNormal2.setColor(normalColor);

        Drawable[] drawables=new Drawable[2];
        drawables[0]=centerButttonNormal1;
        drawables[1]=centerButttonNormal2;
        LayerDrawable layerDrawable = new LayerDrawable(drawables);

        layerDrawable.setLayerInset(1,0,strokeWidth,0,strokeWidth);

        //layerDrawable就是中间的图片的背景普通的样子；


        //中间按钮的selected状态；填充完颜色；
        GradientDrawable centerButttonSelected= new GradientDrawable();
        centerButttonSelected.setStroke(strokeWidth,selectColor);
        centerButttonSelected.setColor(selectColor);

        return createStateListDrawable(layerDrawable,centerButttonSelected);
    }


    /**
     * 创建一个没有左边框的背景选择器；
     * @param selectColor
     * @param normalColor
     * @return
     */
    public Drawable createNoLeftStoke(int selectColor,int normalColor){


        //======创建中间按钮的背景；========
        //中间按钮的normal状态；
        GradientDrawable centerButttonNormal1= new GradientDrawable();
        centerButttonNormal1.setStroke(strokeWidth,selectColor);
        //centerButttonNormal.setStroke();
        centerButttonNormal1.setColor(normalColor);

        //中间按钮的normal状态；
        GradientDrawable centerButttonNormal2= new GradientDrawable();
        //centerButttonNormal.setStroke();
        centerButttonNormal2.setColor(normalColor);

        Drawable[] drawables=new Drawable[2];
        drawables[0]=centerButttonNormal1;
        drawables[1]=centerButttonNormal2;
        LayerDrawable layerDrawable = new LayerDrawable(drawables);

        layerDrawable.setLayerInset(1,0,strokeWidth,strokeWidth,strokeWidth);

        //layerDrawable就是中间的图片的背景普通的样子；


        //中间按钮的selected状态；填充完颜色；
        GradientDrawable centerButttonSelected= new GradientDrawable();
        centerButttonSelected.setStroke(strokeWidth,selectColor);
        centerButttonSelected.setColor(selectColor);

        return createStateListDrawable(layerDrawable,centerButttonSelected);
    }


    public StateListDrawable createStateListDrawable(Drawable normalDrawable,Drawable selectedDrawable){

        StateListDrawable stateListDrawable=new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_selected},selectedDrawable);
        stateListDrawable.addState(new int[]{},normalDrawable);

        return stateListDrawable;
    }



    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public  int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
